<?php
/*
---------------------------------------------------------------------------
status/overview_data.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require_once("../../library/frontend.php");

$location_result = run_query("SELECT locations.id, locations.name FROM locations, hosts WHERE locations.id = hosts.location_id GROUP BY hosts.location_id ORDER BY COUNT(hosts.id) DESC");
if (mysql_num_rows($location_result) != NULL) {
	while(list($location_id, $location_name) = mysql_fetch_array($location_result))	{
		echo '<div class="loc_status"><h3>', $location_name, '</h3>
		<table class="status">';
		$group_result = run_query("SELECT id, name FROM groups ORDER BY name ASC");
		if (mysql_num_rows($group_result) != NULL) {
			while(list($group_id, $group_name) = mysql_fetch_array($group_result))	{
				$host_result = run_query("SELECT id, name FROM hosts WHERE location_id = '$location_id' AND group_id = '$group_id' ORDER BY name ASC");
				if (mysql_num_rows($host_result) != NULL) {
					echo '<tr><th colspan=5>', $group_name, '</th></tr>
					<tr><th style="width:200px;">Host:</th><th style="width:50px;">OK</th><th style="width:50px;">Warning</th><th style="width:50px;">Critical</th><th width="50px">Error</th></tr>';
					while(list($host_id, $host_name) = mysql_fetch_array($host_result))	{
						$ok_result = run_query("SELECT COUNT(id) FROM hosts_triggers WHERE host_id = '$host_id' AND current_state = 0");
						list($ok_count) = mysql_fetch_array($ok_result);
						$warning_result = run_query("SELECT COUNT(id) FROM hosts_triggers WHERE host_id = '$host_id' AND current_state = 1");
						list($warning_count) = mysql_fetch_array($warning_result);
						$critical_result = run_query("SELECT COUNT(id) FROM hosts_triggers WHERE host_id = '$host_id' AND current_state = 2");
						list($critical_count) = mysql_fetch_array($critical_result);
						$error_result = run_query("SELECT COUNT(id) FROM hosts_triggers WHERE host_id = '$host_id' AND current_state > 2");
						list($error_count) = mysql_fetch_array($error_result);
						echo '<tr><td><a href="host.php?id=', $host_id, '">', $host_name, '</a></td><td style="text-align:center;"';
						if ($ok_count != 0)
							echo ' class="OK"';
						echo '">', $ok_count, '</td><td style="text-align:center;"';
						if ($warning_count != 0)
							echo ' class="Warning"';
						echo '">', $warning_count, '</td><td style="text-align:center;"';
						if ($critical_count != 0)
							echo ' class="Critical"';
						echo '">', $critical_count, '</td><td style="text-align:center;"';
						if ($error_count != 0)
							echo ' class="ERROR"';
						echo '">', $error_count, '</td></tr>';
					}
				}
			}
		}
		echo '</table></div>';
	}
}
echo '<div class="current_time">Current Date/Time: ', date("D M j G:i:s T Y"), '</div>';
?>